<template>
	<view>
		<view class="page">
			<view class="cardView">
				<view v-for="(item, index) in remarkList" :key="index" class="remark-item">
					<text class="remark-text">{{ item.label }}</text>
				</view>
			</view>

			<view class="cardView">
				<view class="dashed-box">
							<view class="content-wrapper">
								<text class="label-text">被核查企业名称(盖章):</text>
								<input v-model="selectedRow.operatorName"  class="label-line" placeholder="点击编辑" />
							</view>
				
				<!-- 企业负责人 -->
				<SignatureItem label="企业负责人(签字):" :sign-image.sync="selectedRow.foodPersonSign"
						:date.sync="selectedRow.foodPersonDate"  />
				
				<!-- 核查组长 -->
				<SignatureItem label="核查组长(签字):" :sign-image.sync="selectedRow.checkPersonnelSign"
					:date.sync="selectedRow.checkSignDate"/>
						
				
					</view>
			<text class="remark-text">注：本核查纪律由核查人员在首次会议中现场宣读及解释。</text>
			</view>
		</view>

		<view v-if="editStatus!='FALSE'" class="button-container">
			<view class="button-save">
				<button style="flex: 1;" type="primary" @click="submitForm('1')">保存并提交</button>
			</view>
			<view class="button-save">
				<button style="flex: 1;" type="primary" @click="submitForm('0')">暂存</button>
			</view>
		</view>

	</view>
</template>

<script>
	import SignatureItem from '@/components/signature/signature-item.vue'
	import {
	categoryInfo,
	addCategory,
	getMedicalDict,
	categoryUpdate
	} from '@/api/order';
	export default {
		components: {
			SignatureItem
		},
		
		data() {
			return {
				selectedDateRange: [], // 存储日期范围数组
				editStatus: 'ADD',
				remarkList: [], //文本
				selectedRow: {
					bidType: '',
					operatorName: '',
					foodPersonSign: '',
					foodPersonDate: '',
					checkPersonnelSign: '',
					checkSignDate: '',
					workOrderId: '',
					createBy: "",
					auditStatus: "",
					orderCategory:""
				},
			}
		},
		onLoad(options) {
			if (options.editStatus != null) {
				const editStatus = decodeURIComponent(options.editStatus);
				const workOrderId = decodeURIComponent(options.workOrderId);
				this.selectedRow.workOrderId = workOrderId;
				console.log('this.selectedRow.workOrderId'+this.selectedRow.workOrderId)
				this.editStatus = editStatus;
				this.getOrderInfo(workOrderId);
			} else {
				if (options.orderData) {
					const orderData = JSON.parse(decodeURIComponent(options.orderData));
					this.selectedRow.bidType = orderData.bidType;
					this.selectedRow.workOrderId = orderData.workOrderId;
					this.selectedRow.operatorName = orderData.operatorName;
					this.selectedRow.orderCategory = orderData.orderCategory;
					
				}
			}

			this.getMedicalDict('inspect_discipline_wenan');

		},
		methods: {
			// 获取工单详情
			async getOrderInfo(orderId) {
				
				const res = await categoryInfo(orderId);
				if (res.code === 0) {
					this.selectedRow = res.data;
				}
			},
			//获取字典值
			async getMedicalDict(type) {
				try {
					const res = await getMedicalDict(type)

					// 增加响应状态判断
					if (res.code === 0) {
						switch (type) {
							case 'inspect_discipline_wenan':
								this.remarkList = res.data;
								console.log("inspect_discipline_wenan")
								console.log(res.data)
								break;

							default:
								return;
						}
					} else {
						throw new Error(res?.data?.msg || '接口返回异常')
					}

				} catch (error) {
					uni.showToast({
						title: error.message || '获取分类失败',
						icon: 'none',
						duration: 2000
					})
				}
			},

			handleDateConfirm(date) {
				this.$set(this.selectedRow, this.currentDateType, date)
			}, // 提交方法

			async submitForm(status) {
				try {
					// 状态校验前置
					if (this.editStatus === 'FALSE') {
						return uni.showToast({
							title: '当前处于查看状态',
							icon: 'none'
						});
					}
					
					
					// 统一处理提交逻辑
					const isEditMode = this.editStatus === 'EDIT';
					const apiMethod = isEditMode ? categoryUpdate : addCategory;
					// 构造请求参数（避免污染原始数据）
					const payload = {
						...this.selectedRow,
						auditStatus: status,
					};

					// 显示加载状态
					uni.showLoading({
						title: '提交中...',
						mask: true
					});

					console.log(payload)
					// 执行API调用
					const res = await apiMethod(payload);

					if (res.code !== 0) {
						throw new Error(res.msg || '服务器处理异常');
					}

					// 统一处理成功逻辑
					uni.showToast({
						title: '提交成功',
						icon: 'success'
					});

					// 关闭当前页面，跳转到工单页
					this.$closeAndRedirect({
					  url: '/pages/work/history/index'
					});

				} catch (error) {
					// 统一错误处理
					console.error('提交失败:', error);
					uni.showToast({
						title: error.msg || '请求失败，请检查网络',
						icon: 'none',
						duration: 3000
					});
				} finally {
					uni.hideLoading();
				}
			},

		}
	}
</script>


<style>
	.page {
		background-color: #e9f1fe;
		padding: 20px;
	}

	.cardView {
		margin-bottom: 10px;
		padding: 15px;
		background: #ffffff;
		border-radius: 8px;
		box-shadow: 0 2px 12px rgba(0, 0, 0, 0.12);
	}

	.remark-text {
		font-size: 15px;
		line-height: 1.6;
		margin-top: 18px;
		/* 关键行：设置行高 */
		display: block;
		/* 确保独占一行，行高生效 */
		margin-bottom: 18px;
		text-indent: 2em
			/* 可选：段落间距 */
	}


	.commitment-container {
		text-indent: 2em;
		font-size: 16px;
	}


	.inline-block {
		display: inline-block;
	}

	.date-box {
		min-width: 4em;
		border-bottom: 1px solid #666;
		padding: 0 8rpx;
	}

	.date-box2 {
		min-width: 4em;
		border-bottom: 1px solid #666;
		padding: 0 8rpx;
		text-align: center;
		font-size: 17px;
		/* 修改方案 */
		padding-top: 5px;
		padding-bottom: 0px;
		/* 减少底部内边距 */
		line-height: 1.1;
		/* 调整行高 */
	}

	/* 如果需要更精确控制输入框位置 */
	.date-box2 input {
		padding-bottom: 1;
		/* 移除输入框自带的内边距 */
		margin-bottom: -2px;
		/* 微调位置 */
	}

	.production-input {
		border-bottom: 1px solid #666;
		padding: 0 8rpx;
		width: 200rpx;
	}

	.button-container {
		display: flex;
		background-color: #ffffff;
		width: 100%;
		padding: 10px;
		flex-direction: column;
		/* 垂直排列 */

		.button-save {
			margin-bottom: 10px;
		}

		.button-child {
			display: flex;
			flex-direction: row;
			/* 垂直排列 */

		}
	}

	.edit-item {
		display: inline-block;
		min-width: 80rpx;
		border-bottom: 1px solid #000000;
		color: #2e31ee;
		padding: 0 10rpx;
		margin: 0 8rpx;
	}

	/* 虚线容器 */
	.dashed-box {
		width: 100%;
		margin: 24rpx auto;
		padding: 12rpx;
		border: 2rpx dashed #333;
		border-radius: 10rpx;
		background: #fff;
		position: relative;

		&+.dashed-box {
			margin-top: 28rpx;
		}
	}
	

	.content-wrapper {
		width: 100%;
		display: flex;
		align-items: center;
	}
	
	
	.label-text {
		flex: 0 0 40%; /* 占据30%不伸缩 */
		padding-right: 20rpx;
		font-size: 18px;
		text-align: right;
		white-space: nowrap;
	}
	
	.label-line {
		flex: 1; /* 占据剩余70%空间 */
		min-height: 60px;
		padding-left: 20px;
		padding-top: 20px;
		margin-top: 10px;
		border-bottom: 2rpx solid #666 !important;
		margin-bottom: 40px;
		margin-right: 20%;
	}
</style>